#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "md5.h"

using namespace std;

char* verify (void)
{
	// Available interface names
	vector<string> intName = vector<string>();
	// MAC addresses
	vector<string> strMac = vector<string>();
	// Concatenated macs
	string conMacs = "";
	// Pointer to directory object
	DIR *dp = NULL;
	// Pointer to directory entry
	struct dirent *dir_entry = NULL;
	// Path to directory
	const string path = "/sys/class/net";
	const string noMac  = "nop";

	// Open directory and read its contents
	if((dp  = opendir(path.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << path << endl;
        return (char *)noMac.c_str();
    }
	while ((dir_entry = readdir(dp)) != NULL) {
		if (((string)dir_entry->d_name) == "." ||
		     ((string)dir_entry->d_name) == "..")
				 continue;
        intName.push_back(string(dir_entry->d_name));
    }
    closedir(dp);
	unsigned int i = 0;
	for(; i<intName.size(); i++)
	{
		// Form path string to the target file which contains
		// interface mac address
		string mac_path("/sys/class/net/"+intName[i]+"/address");
		string mac_address = "";
		ifstream is(mac_path.c_str());
		getline (is, mac_address);
		conMacs.append(mac_address);
		strMac.push_back (mac_address);
	}
	//cout << "Concatenated macs: "<< conMacs << endl;
	// Create md5 object
	MD5 md5;
	//cout << "MD5: "<< md5.digestString((char*)conMacs.c_str()) << endl;
	return md5.digestString((char*)conMacs.c_str());
}